// import parse from '../../utils/parse'
import * as api from '../service'
import * as utils from '../utils'
import _ from 'lodash'
export default {
  name: 'exParser',
  inject: ['components', 'rootPage'],
  data() {
    return {
      cachedExp: {},
      cachedFn: {}
    }
  },
  beforedDestory() {
    if (this.config.name && this.components[this.config.name] === this) {
      delete this.components[this.config.name]
    }
  },
  created() {
    if (this.config.name && !this.components[this.config.name]) {
      this.components[this.config.name] = this
    }
  },
  methods: {
    getParsedEx(name, notInConfig, row, index) {
      return () => {
        let content
        if (notInConfig) {
          content = name
        } else {
          content = _.at(this.config, name)[0]
        }
        let fn = new Function('$data', '$dialogData', '$row', '$index', '$refs', '$api', '$route', '$message', '$globalState', '$utils', '$pageState', '$setPageState', `return ${content};`)
        try {
          return fn(this.rootForm.properties, this.dialogProperties, row || this.scope, index, this.components, api, this.$route, this.$message, this.$store.state, utils, this.rootPage && this.rootPage.state, this.rootPage && this.rootPage.setState)

        } catch (e) {
          return undefined
        }
      }
    },
    getParsedFn(name, notInConfig, row, index) {
      let content
      if (notInConfig) {
        content = name
      } else {
        content = _.at(this.config, name)[0]
      }
      let fn = new Function('$data', '$dialogData', '$row', '$index', '__arguments', '$refs', '$api', '$route', '$message', '$globalState', '$utils', '$pageState', '$setPageState', `return (${content}).apply(this,__arguments);`)
      return (...rest) => {
        return fn(this.rootForm.properties, this.dialogProperties, row || this.scope, index, rest, this.components, api, this.$route, this.$message, this.$store.state, utils, this.rootPage && this.rootPage.state, this.rootPage && this.rootPage.setState)
      }
    }
  }
}
